﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Moving03.Classes;

namespace Moving03
{
    public partial class WebForm7 : BasePage
    {
        DataClasses1DataContext context = new DataClasses1DataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            ThongTinVanChuyen ttvc = (ThongTinVanChuyen)Session["ttvc"];
            NoiDen noiDen = (NoiDen)Session["noiDen"];
            NoiDi noiDi = (NoiDi)Session["noiDi"];
            LoHang loHang = (LoHang)Session["loHang"];




            //////////////////////////////////////////////////

            MatHang matHangGiong = (MatHang)Session["matHangGiong"];

            float TongTien = returnGia((int)noiDen.Id_Quocgia, (int)noiDi.Id_Quocgia, (int)ttvc.Id_PhuongThucGoiHang,(int)loHang.TongTrong_luong);
            float TongChiPhi = TongTien;
            ChiTietVanChuyen ctvc = new ChiTietVanChuyen();
            ctvc.TienChiuTacDong = 0;
            ctvc.TienKiGoi = 0;

            ctvc.TienGoi = TongTien;
            if (ttvc.TacDongKhiHau == 1)
            {
                ctvc.TienChiuTacDong = 5 * TongTien / 100;
                TongChiPhi += (float)ctvc.TienChiuTacDong;
            }
            if (ttvc.DungKiGoi == 1)
            {
                ctvc.TienKiGoi = 5 * TongTien / 100;
                TongChiPhi += (float)ctvc.TienKiGoi;
            }
            ttvc.TongTien = TongChiPhi;
            lblTongchiphi.Text = TongChiPhi.ToString() + "vnđ";
            //////////////////////////////

            aspnet_User user = context.aspnet_Users.Single(i => i.UserId == ttvc.Id_User);
            Label1.Text = user.TenCongTy;
            Label2.Text = user.HoTen;
            DanhSachQuocGia getQG = context.DanhSachQuocGias.Single(i => i.Id_MaQGia == noiDi.Id_Quocgia);
            Label3.Text = getQG.TenQuocGia;
            Label4.Text = user.DiaChi1;
            Label5.Text = noiDen.Thanhpho;
            Label6.Text = noiDen.Mabuudien.ToString();
            Label7.Text = noiDen.Dienthoai.ToString();
            Label8.Text = noiDen.Email.ToString();

            Label9.Text = ttvc.TenDoanhNghiep;
            Label10.Text = ttvc.TenLienLac;
            DanhSachQuocGia qg = context.DanhSachQuocGias.Single(i => i.Id_MaQGia == noiDen.Id_Quocgia);
            Label11.Text = qg.TenQuocGia;
            Label12.Text = noiDen.Diachi1;
            Label13.Text = noiDen.Diachi2;
            Label14.Text = noiDen.Thanhpho;
            Label15.Text = noiDen.Mabuudien.ToString();
            Label16.Text = noiDen.Dienthoai.ToString();
            Label17.Text = noiDen.Email;
            if (noiDen.Nharieng == 1)
            {
                Label18.Text = "Đúng";
            }
            else
            {
                Label18.Text = "Khác";
            }
            Label19.Text = ttvc.TenNeuSuCo;
            Label20.Text = ttvc.DiaChiGoiNeuSuCo;
            Label21.Text = loHang.Ten_lo_hang;
            Label22.Text = loHang.So_goi.ToString();
            if (loHang.Giong_nhau == 1)
            {
               // MatHang matHangGiong = (MatHang)Session["matHangGiong"];
                Label23.Text = "Đúng";
                Label24.Text = matHangGiong.TenMatHang;
                Label25.Text = matHangGiong.TrongLuong.ToString();
                Label26.Text = loHang.TongTrong_luong.ToString();
                Label27.Text = loHang.Gia_tri.ToString();
            }
            else
            {
                Label23.Text = "Khác";
                Label24.Text = "Không có";
                Label25.Text = "Không có";
                //for(int i = 0; i < 
            }
           
            PhuongThucGoiHang ptgh = context.PhuongThucGoiHangs.Single(i => i.Id_GoiHang == ttvc.Id_PhuongThucGoiHang);
            Label28.Text = ptgh.LoaiPhat;
          
            Label29.Text = ttvc.ThongTinBiMat1;
            Label30.Text = ttvc.ThongTinBiMat2;
            if (ttvc.NhanEmail == 1)
            {
                Label31.Text = "Có";
            }
            else
            {
                Label31.Text = "Không";
            }
            if (ttvc.DungKiGoi == 1)
            {
                Label32.Text = "Có";
            }
            else
            {
                Label32.Text = "Không";
            }
            if (ttvc.TacDongKhiHau == 1)
            {
                Label33.Text = "Có";
            }
            else
            {
                Label33.Text = "Không";
            }
            PhuongThucThanhToan ctsp = context.PhuongThucThanhToans.Single(i => i.Id_ThanhToan == ttvc.Id_PhuongThucThanhToan);
            Label34.Text = ctsp.TenThanhToan;

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            MatHang matHangGiong = (MatHang)Session["matHangGiong"];
            if (matHangGiong != null)
            {
                Response.Redirect("Default.aspx");
            }
            else 
            {
                LoHang loHang = (LoHang)Session["loHang"];
                Response.Redirect("ThongTinLoHang.aspx?smh=" + loHang.So_goi.ToString() + "&idlh=" + loHang.Id_LoHang.ToString());
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            ThongTinVanChuyen ttvc = (ThongTinVanChuyen)Session["ttvc"];
            NoiDen noiDen = (NoiDen)Session["noiDen"];
            NoiDi noiDi = (NoiDi)Session["noiDi"];
            LoHang loHang = (LoHang)Session["loHang"];
            

            context.NoiDens.InsertOnSubmit(noiDen);
            context.SubmitChanges();
            int maNoiDen = noiDen.Id_NoiDen;

            context.NoiDis.InsertOnSubmit(noiDi);
            context.SubmitChanges();
            int maNoiDi = noiDi.Id_Noidi;

            context.LoHangs.InsertOnSubmit(loHang);
            context.SubmitChanges();
            int maLoHang = loHang.Id_LoHang;
            float tongtrongluong = (float)loHang.TongTrong_luong;
            MatHang matHangGiong = (MatHang)Session["matHangGiong"];
            if (matHangGiong != null)
            {
                matHangGiong.Id_LoHang = maLoHang;
                context.MatHangs.InsertOnSubmit(matHangGiong);
                context.SubmitChanges();
            }
            else
            {
               
                for (int i = 0; i < loHang.So_goi; i++)
                {
                    MatHang matHang = (MatHang)Session["matHang"+i];
                    matHang.Id_LoHang = maLoHang;
                    tongtrongluong += (float)matHang.TrongLuong;
                    context.MatHangs.InsertOnSubmit(matHang);
                    context.SubmitChanges();
                }
                LoHang lohangupdate = context.LoHangs.Single(p => p.Id_LoHang == maLoHang);
                lohangupdate.TongTrong_luong = tongtrongluong;
                context.SubmitChanges();
            }
            ttvc.Id_LoHang = maLoHang;
            ttvc.IdNoiden = maNoiDen;
            ttvc.IdNoiDi = maNoiDi;
            ttvc.TinhTrang = 1;
            ttvc.NgayDat = DateTime.Now;
            ThongTinVanChuyen ttvc2 = (ThongTinVanChuyen)Session["ttvc"];
            int ngayDuKien = KhoangNgayDuDoan((int)noiDen.Id_Quocgia,(int)noiDi.Id_Quocgia, (int)ttvc2.Id_PhuongThucGoiHang);
            ttvc.TongTien = 0;
            ttvc.DaThanhToan = 0;
            ttvc.NgayToi = DateTime.Now.AddDays(ngayDuKien);
           

            float TongTien = returnGia((int)noiDen.Id_Quocgia, (int)noiDi.Id_Quocgia, (int)ttvc2.Id_PhuongThucGoiHang, tongtrongluong);
            float TongChiPhi = TongTien;
            ChiTietVanChuyen ctvc = new ChiTietVanChuyen();
            ctvc.TienChiuTacDong = 0;
            ctvc.TienKiGoi = 0;

            ctvc.TienGoi = TongTien;
            if(ttvc.TacDongKhiHau ==1)
            {
                ctvc.TienChiuTacDong = 5 * TongTien/100;
                TongChiPhi += (float)ctvc.TienChiuTacDong;
            }
            if (ttvc.DungKiGoi == 1)
            {
                ctvc.TienKiGoi = 5 * TongTien / 100;
                TongChiPhi += (float)ctvc.TienKiGoi;
            }
            ttvc.TongTien = TongChiPhi;
            //lblTongchiphi.Text = TongChiPhi.ToString();
            context.ThongTinVanChuyens.InsertOnSubmit(ttvc);
            context.SubmitChanges();
            
            ctvc.Id_TTVC = ttvc.Id_TTVC;
           
            context.ChiTietVanChuyens.InsertOnSubmit(ctvc);
            context.SubmitChanges();
            int id_ddcn = insertDiaDiemCapNhat(ttvc.Id_TTVC);
            insertDuongDiChoLoHang(ttvc.Id_TTVC, id_ddcn);
            Response.Redirect("XemQuaTrinhVanChuyen1.aspx");    
        }
        public void insertDuongDiChoLoHang(int id_TTVC, int id_ddcn)
        {
            int id_noidi = 0;
            int id_noiden = 0;
            var query = from ttvc in context.ThongTinVanChuyens
                        join noidi in context.NoiDis on ttvc.IdNoiDi equals noidi.Id_Noidi
                        join noiden in context.NoiDens on ttvc.IdNoiden equals noiden.Id_NoiDen
                        where ttvc.Id_TTVC == id_TTVC
                        select new { id_noidi = noidi.Id_Quocgia, id_noiden = noiden.Id_Quocgia };


            foreach (var thongtin in query)
            {
                id_noidi = (int)thongtin.id_noidi;
               
             
                id_noiden = (int)thongtin.id_noiden;
                
            }

            var query1 = from cpddnn in context.ChiPhiDiaDiemNuocNgoais
                         join ddtg1t in context.DiaDiemTrungGian1Tuyens on cpddnn.Id_CPDDNNgoai equals ddtg1t.Id_CPDDNNgoai
                         join dd in context.DiaDiemTrungGians on ddtg1t.Id_TG equals dd.Id_DDTrungGian
                         where cpddnn.Id_QuocGia1 == id_noidi && cpddnn.Id_QuocGia2 == id_noiden
                         select new  {dd,ddtg1t.thutuvanchuyen };

            foreach (var dd in query1)
            {
                DuongDi duongdi = new DuongDi();
                duongdi.DaToiChua = 0;
                duongdi.Id_DDTGian = dd.dd.Id_DDTrungGian;
                duongdi.Id_DDCapNhat = id_ddcn;
                duongdi.SoThuTu = dd.thutuvanchuyen;
                context.DuongDis.InsertOnSubmit(duongdi);
                context.SubmitChanges();

            }

        }

        private int insertDiaDiemCapNhat(int id_TTVC)
        {
            DiaDiemCapNhat ddcn = new DiaDiemCapNhat();
            ddcn.Id_ThongTinVanChuyen = id_TTVC;
            context.DiaDiemCapNhats.InsertOnSubmit(ddcn);
            context.SubmitChanges();
            int id_diadiemcapnhat = ddcn.Id_DDCapNHat;
            return id_diadiemcapnhat;
        }

        public int KhoangNgayDuDoan(int idquocgia1, int idquocgia2, int loaivanchuyen)
        {
            int ngay = 0;
            var query3 = from cp in context.ChiPhiDiaDiemNuocNgoais
                         where cp.Id_QuocGia1 == idquocgia1 && cp.Id_QuocGia2 == idquocgia2
                         select cp
                         ;
            if (query3.Count() == 0)
            {
                query3 = from cp in context.ChiPhiDiaDiemNuocNgoais
                         where cp.Id_QuocGia1 == idquocgia2 && cp.Id_QuocGia2 == idquocgia1
                         select cp;
            }
            foreach(var cp in query3)
            {
                if (loaivanchuyen == 1)
                    return (int)cp.NgayGoiHangNhanh;
                else
                    return (int)cp.NgayGoiHangBinhThuong;
            }
            return ngay;
        }

        public float returnGia(int idmaquocgia1, int idmaquocgia2, int loaigoihang, float trongLuong)
        {
            float gia = 0;
            var query3 = from cp in context.ChiPhiDiaDiemNuocNgoais
                         where cp.Id_QuocGia1 == idmaquocgia1 && cp.Id_QuocGia2 == idmaquocgia2
                         select cp;
            if (query3.Count() == 0)
            {
                query3 = from cp in context.ChiPhiDiaDiemNuocNgoais
                         where cp.Id_QuocGia1 == idmaquocgia2 && cp.Id_QuocGia2 == idmaquocgia1
                         select cp;
            }
            if (query3.Count() > 0)
            {
                foreach (var x in query3)
                {
                    var query4 = from ctcp in context.ChiTietChiPhiNuocNgoais
                                    where ctcp.Id_ChiPhi == x.Id_CPDDNNgoai
                                    select ctcp;
                    foreach (var chitietgia in query4)
                    {
                        if (trongLuong-chitietgia.TrongLuong < 0.5)
                        {
                            if (loaigoihang == 1)
                            {
                                return (float)chitietgia.GiaChuyenNhanh;
                            }
                            if (loaigoihang == 2)
                            {
                                return (float)chitietgia.GiaChuyenThuong;
                            }
                        }
                        if (loaigoihang == 1)
                        {
                            gia = (float)(chitietgia.GiaChuyenNhanh / chitietgia.TrongLuong) * trongLuong;
                        }
                        if (loaigoihang == 2)
                        {
                            gia = (float)(chitietgia.GiaChuyenThuong / chitietgia.TrongLuong) * trongLuong;
                        }
                    }

                }                
            }
            return gia;
        }
    }
}